package com.system.service.impl;

import com.system.mapper.SysRoleMapper;
import com.system.mapper.SysUserMapper;
import com.system.pojo.SysRole;
import com.system.pojo.SysUser;
import com.system.service.SysUserService;
import com.system.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {

	@Autowired
	private SysUserMapper sysUserMapper;

	@Autowired
	private SysRoleMapper sysRoleMapper;

	@Override
	public ResultVo<SysUser> selectByUsername(String username) {
		return null;
	}

	@Override
	public ResultVo<SysUser> login(SysUser sysUser) {
		ResultVo<SysUser> resultVo = new ResultVo<SysUser>();
		SysUser sysUser1 = sysUserMapper.selectByUsername(sysUser.getUsername());
		if (sysUser1 == null) {
			// 没有查询到用户，用户名不存在
			resultVo.setMessage("用户名不存在");
		} else {
			if (!sysUser.getPassword().equals(sysUser1.getPassword())) {
				resultVo.setMessage("用户密码不正确");
			} else {
				resultVo.setSelect(true);
				resultVo.setT(sysUser1);
			}
		}
		return resultVo;

	}

	@Override
	public ResultVo<SysUser> selectAllSaler() {
		ResultVo<SysUser> resultVo = new ResultVo<SysUser>();
		List<SysUser> sysUsers = null;
		List<SysUser> sysUsers2 = new ArrayList<>();
		sysUsers = sysUserMapper.selectAllSaler();
		for (SysUser sysUser : sysUsers) {
			SysRole sysRole = sysUser.getSysRole();
			if (sysRole.getRoleName().equals("ROLE_SALER")) {
				sysUsers2.add(sysUser);
			}
		}
		resultVo.setTList(sysUsers2);
		resultVo.setSelect(true);
		return resultVo;
	}

	@Override
	public ResultVo<SysUser> salerInsert(SysUser sysUser) {
		ResultVo<SysUser> resultVo = new ResultVo<SysUser>();
		SysUser sysUser1 = sysUserMapper.selectByUsername(sysUser.getUsername());
		if (sysUser1 == null) {
			SysRole sysRole = sysRoleMapper.selectByRoleName("ROLE_SALER");
			Integer roleId = sysRole.getId();
			sysUser.setRoleId(roleId);
			int i = sysUserMapper.insertSelective(sysUser);
			if (i == 1) {
				resultVo.setInsert(true);
				resultVo.setMessage("添加成功");
				return resultVo;
			}
			throw new RuntimeException();
		} else {
			resultVo.setMessage("该用户名已存在");
		}
		return resultVo;
	}

	@Override
	public ResultVo<SysUser> selectAll() {
		ResultVo<SysUser> resultVo = new ResultVo<SysUser>();
		List<SysUser> sysUsers = sysUserMapper.selectAll();
		resultVo.setTList(sysUsers);
		resultVo.setSelect(true);
		resultVo.setMessage("查询成功");
		return resultVo;
	}

	@Override
	public ResultVo<SysUser> update(SysUser sysUser) {
		ResultVo<SysUser> resultVo = new ResultVo<SysUser>();
		String roleName = sysUser.getSysRole().getRoleName();
		SysRole sysRole = sysRoleMapper.selectByRoleName(roleName);
		Integer rid = sysRole.getId();
		sysUser.setRoleId(rid);
		sysUserMapper.updateByPrimaryKeySelective(sysUser);
		resultVo.setUpdate(true);
		resultVo.setMessage("更新成功");
		return resultVo;
	}

	@Override
	public ResultVo<SysUser> insert(SysUser sysUser) {
		ResultVo<SysUser> resultVo = new ResultVo<SysUser>();
		SysUser sysUser1 = sysUserMapper.selectByUsername(sysUser.getUsername());
		if (sysUser1 == null) {// 不存在该用户，可以添加
			SysRole sysRole = sysRoleMapper.selectByRoleName(sysUser.getSysRole().getRoleName());
			Integer rid = sysRole.getId();
			sysUser.setRoleId(rid);
			int i = sysUserMapper.insertSelective(sysUser);
			if (i == 1) {
				resultVo.setInsert(true);
				resultVo.setMessage("添加成功");
			} else {
				throw new RuntimeException();
			}
		} else {
			resultVo.setMessage("该用户名已存在");
		}
		return resultVo;
	}

	@Override
	public ResultVo<SysUser> selectById(Integer userId) {
		ResultVo<SysUser> resultVo = new ResultVo<SysUser>();
		SysUser sysUser = sysUserMapper.selectByPrimaryKey(userId);
		if (sysUser == null) {
			resultVo.setMessage("查询失败");
		} else {
			resultVo.setT(sysUser);
			resultVo.setSelect(true);
			resultVo.setMessage("查询成功");
		}
		return resultVo;
	}

	@Override
	public ResultVo<SysUser> salerUpdate(SysUser sysUser) {
		ResultVo<SysUser> resultVo = new ResultVo<SysUser>();
//		SysUser sysUser1 = sysUserMapper.selectByUsername(sysUser.getUsername());
//		if (sysUser1 == null) {
		int i = sysUserMapper.updateByPrimaryKeySelective(sysUser);
		if (i == 1) {
			resultVo.setUpdate(true);
			resultVo.setMessage("更新成功");
		} else {
			throw new RuntimeException();
		}
//		} else {
//		resultVo.setMessage("该用户名已存在");
//		}
		return resultVo;
	}

	@Override
	public ResultVo<SysUser> deleteById(Integer adminId) {
		ResultVo<SysUser> resultVo = new ResultVo<SysUser>();
		int i = sysUserMapper.deleteById(adminId);
		resultVo.setDelete(true);
		resultVo.setMessage("删除成功");
		return resultVo;
	}
}
